<?php

Loader::using("ext::libs::MicrosoftDateFormatter");
Loader::using("models::entities::Inscritos");
Loader::using("models::entities::Personas");
Loader::using("models::entities::DatosContacto");
Loader::using("models::entities::HistorialLaboral");
Loader::using("ext::libs::EmailsAdmisiones");

/**
 * Description of ActualizarDatos
 * 
 * @author 
 * @export Modules
 * @export-metadata default-module
 */
class ActualizarNuevoEstudiante extends Module {

    public function index() {
        $this->loadPersonalData();
        $this->loadCarrers();
        $this->loadBarrios();

        $this->display("index");
    }

    public function Create() {
        if ($this->request->submitted()) {
            /* @var $inscrito Inscritos */
            $inscrito = Asociados::fillMissingData(new Inscritos());
            $this->request->bind($inscrito);

            $newPass = $this->model->admisiones->Create($inscrito);

            $this->addMessage(new Message("Sus datos personales han sido actualizados.", "", Message::SUCCESS));

            $this->assign("action", $this->route->moduleAction("Entrevistas"));
            $this->assign("password", $newPass);

            $this->sendMail($inscrito);
            
            $this->display("Update");
        } else {
            $this->index();
        }
    }

    private function loadPersonalData() {
        $this->assign("estadosCiviles", Asociados::$EstadosCiviles);
        $this->assign("tiposDocumentos", Identificacion::TiposDocumentos());
        $this->assign("estratos", array("1", "2", "3", "4", "5", "6",));
        $this->assign("tiposDeSangre", TipoSangre::$nombresGrupos);
        $this->assign("RHs", array("+", "-"));

        $isEgresado = $this->request->get("NoFrame", 0);
        $this->assign("action", $this->route->action("Create", array("NoFrame" => $isEgresado,)));

        $this->assign("viewLaboral", $this->route->moduleAction("InformacionLaboral", "ShowInformacionLaboral"));
        $this->assign("viewAcademico", $this->route->moduleAction("InformacionAcademica", "ShowInformacionAcademica"));
        $this->assign("viewIdiomas", $this->route->moduleAction("InformacionIdiomas", "ShowInformacionIdiomas"));

        $this->assign("Paises", $this->model->countries->GetAll(false));
    }

    private function loadBarrios() {
        $this->assign("Barrios", $this->model->admisiones->GetBarrios());
    }

    private function loadCarrers() {
        $carreras = array();
        foreach (TipoPrograma::values() as $key => $value) {
            $programas = $this->model->career->GetByTipoPrograma($value, false);
            if (count($programas)) {
                $carreras[$key] = $programas;
            }
        }

        $this->assign("Carreras", $carreras);
    }

    private function sendMail(Inscritos $incrito) {
        $emails = array();
        array_push($emails, $incrito->getEmail());
        
        $params = array();
        
        $nombreCompleto = $incrito->getApellidos().', '. $incrito->getNombres();
        
        array_push($params, $nombreCompleto);
        array_push($params, "");
        array_push($params, date('d-M-Y'));
        
        /* @var $planEstudio PlanesEstudios */
        $planEstudio = $this->model->career->getById($incrito->getPlanEstudio());
        
        array_push($params, $planEstudio->getNombrePlan());
        
        EmailsAdmisiones::SendMail(1, $emails, $params);
    }

}

?>
